<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\CreateLayoutRequest;
use App\Http\Requests\UpdateLayoutRequest;
use App\Models\Category;
use App\Models\Goods;
use App\Repositories\LayoutRepository;
use App\Http\Controllers\AppBaseController;
use App\Repositories\PicturesRepository;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

class LayoutController extends AppBaseController
{
    /** @var  LayoutRepository */
    private $layoutRepository;
    private $pic;

    public function __construct(LayoutRepository $layoutRepo, PicturesRepository $picturesRepository)
    {
        $this->layoutRepository = $layoutRepo;
        $this->pic = $picturesRepository;
    }

    /**
     * Display a listing of the Layout.
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->layoutRepository->pushCriteria(new RequestCriteria($request));
        $layouts = $this->layoutRepository->makeModel()->orderBy('sort','asc')->get();

        $goods = Goods::all();
        $goods_arr = [];
        foreach ($goods as $item) {
            $goods_arr[$item->id] = $item;
        }

        $category = Category::all();
        $category_arr = [];
        foreach ($category as $item) {
            $category_arr[$item->id] = $item;
        }
        foreach ($layouts as $layout) {
            if($layout->image_type==3){
                if($layout->skip_type==1){
                    if($layout->skip_id)
                        $layout->image = $category_arr[$layout->skip_id]->image;
                    if($layout->skip2_id)
                        $layout->image2 = $category_arr[$layout->skip2_id]->image;
                }elseif($layout->skip_type==2){
                    if($layout->skip_id)
                        $layout->image = $goods_arr[$layout->skip_id]->image;
                    if($layout->skip2_id)
                        $layout->image2 = $goods_arr[$layout->skip2_id]->image;
                }
            }else{

            }
        }
        return view('layouts.index')
            ->with('layouts', $layouts)->with('goods', $goods)->with('category_arr', $category_arr);
    }

    public function setting()
    {
        return view('layouts.setting');
    }

    /**
     * Show the form for creating a new Layout.
     *
     * @return Response
     */
    public function create()
    {
        return view('layouts.create');
    }

    /**
     * Store a newly created Layout in storage.
     *
     * @param CreateLayoutRequest $request
     *
     * @return Response
     */
    public function store(CreateLayoutRequest $request)
    {
        $input = $request->all();

        if($request->file("image")){
            $image = $this->pic->uploadByHash($request->file('image'),"nihaocai",null,null);
            $input['image'] = $image->url;
        }
        if($request->file("image2")){
            $image2 = $this->pic->uploadByHash($request->file('image2'),"nihaocai",null,null);
            $input['image2'] = $image2->url;
        }

        if($request->file("image3")){
            $image2 = $this->pic->uploadByHash($request->file('image3'),"nihaocai",null,null);
            $input['image3'] = $image2->url;
        }
        $layout = $this->layoutRepository->create($input);

        Flash::success('Layout saved successfully.');

        return redirect(route('layouts.index'));
    }

    /**
     * Display the specified Layout.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        $layout = $this->layoutRepository->findWithoutFail($id);

        if (empty($layout)) {
            Flash::error('Layout not found');

            return redirect(route('layouts.index'));
        }

        return view('layouts.show')->with('layout', $layout);
    }

    /**
     * Show the form for editing the specified Layout.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function edit($id)
    {
        $layout = $this->layoutRepository->findWithoutFail($id);

        if (empty($layout)) {
            Flash::error('Layout not found');

            return redirect(route('layouts.index'));
        }

        return view('layouts.edit')->with('layout', $layout);
    }

    /**
     * Update the specified Layout in storage.
     *
     * @param  int              $id
     * @param UpdateLayoutRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateLayoutRequest $request)
    {
        $layout = $this->layoutRepository->findWithoutFail($id);

        if (empty($layout)) {
            Flash::error('Layout not found');

            return redirect(route('layouts.index'));
        }
        $input = $request->all();
        if($request->file("image")){
            $image = $this->pic->uploadByHash($request->file('image'),"nihaocai",null,null);
            $input['image'] = $image->url;
        }
        if($request->file("image2")){

            $image2 = $this->pic->uploadByHash($request->file('image2'),"nihaocai",null,null);
            $input['image2'] = $image2->url;
        }
        if($request->file("image3")){

            $image2 = $this->pic->uploadByHash($request->file('image3'),"nihaocai",null,null);
            $input['image3'] = $image2->url;
        }
        $layout = $this->layoutRepository->update($input, $id);

        Flash::success('Layout updated successfully.');

        return redirect(route('layouts.index'));
    }

    /**
     * Remove the specified Layout from storage.
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        $layout = $this->layoutRepository->findWithoutFail($id);

        if (empty($layout)) {
            Flash::error('Layout not found');

            return redirect(route('layouts.index'));
        }

        $this->layoutRepository->delete($id);

        Flash::success('Layout deleted successfully.');

        return redirect(route('layouts.index'));
    }
}
